package com.github.pig.nideshop.controller.user;


import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pig.common.util.ZyResult;
import com.github.pig.nideshop.entity.*;
import com.github.pig.nideshop.service.*;
import com.netflix.discovery.converters.Auto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  商品控制器
 * </p>
 *
 * @author J神
 * @since 2018-06-11
 */
@RestController
@RequestMapping("/goods")
public class NideshopGoodsController {
    @Autowired
    private INideshopGoodsService nideshopGoodsService;
    @Autowired
    private INideshopGoodsAttributeService nideshopGoodsAttributeService;
    @Autowired
    private INideshopGoodsGalleryService nideshopGoodsGalleryService;
    @Autowired
    private INideshopGoodsIssueService nideshopGoodsIssueService;
    @Autowired
    private INideshopGoodsSpecificationService nideshopGoodsSpecificationService;
    @Autowired
    private INideshopBrandService nideshopBrandService;
    @Autowired
    private INideshopProductService nideshopProductService;
    @Autowired
    private INideshopCollectService nideshopCollectService;
    @Autowired
    private INideshopCartService nideshopCartService;

    /**
     * 商品列表
     * @param nideshopGoods
     * @return
     */
    @GetMapping
    public ZyResult goods(NideshopGoods nideshopGoods, Page page){
        EntityWrapper<NideshopGoods> ex=new EntityWrapper<>();
        ex.setEntity(nideshopGoods);
        return ZyResult.ok(nideshopGoodsService.selectPage(page,ex));
    }

    /**
     * 商品详情
     * @param id
     * @return
     */
    @GetMapping("{id}")
    public ZyResult good(@PathVariable(value = "id") Integer id){
        Map map=new HashMap<>();
        map.put("goods_id",id);
        List<NideshopGoodsAttribute> attributes=nideshopGoodsAttributeService.selectByMap(map);
        List<NideshopGoodsGallery> gallerys=nideshopGoodsGalleryService.selectByMap(map);
        List<NideshopGoodsIssue> issues=nideshopGoodsIssueService.selectByMap(map);
        List<NideshopGoodsSpecification> specifications=nideshopGoodsSpecificationService.selectByMap(map);
        List<NideshopProduct> products=nideshopProductService.selectByMap(map);
        List<NideshopCart> carts=nideshopCartService.selectByMap(map);
        map.clear();
        map.put("value_id",id);
        List<NideshopCollect> collects=nideshopCollectService.selectByMap(map);
        NideshopGoods nideshopGoods=nideshopGoodsService.selectById(id);
        map.clear();
        map.put("attributes",attributes);
        map.put("gallerys",gallerys);
        map.put("issues",issues);
        map.put("specifications",specifications);
        map.put("products",products);
        map.put("collects",collects);
        map.put("info",nideshopGoods);
        return ZyResult.ok(map);
    }
}

